import { createRouter, createWebHashHistory } from 'vue-router'
import store from '@/store'

const routes = [
  {
    path: '/',
    redirect: '/RiskWarning',
    component: () => import('@/layout/home'),
    meta: { needLogin: true },
    children: [
      {
        path: 'Login',
        meta: {
          title: '登录'
        },
        component: () => import('@/layout/login')
      },
      {
        path: 'Home',
        meta: { needLogin: true },
        component: () => import('@/views/Home')
      },
      {
        path: 'RiskWarning',
        meta: { needLogin: true },
        component: () => import('@/views/RiskWarning')
      },
      {
        path: 'A',
        meta: { keepAlive: true, needLogin: true },
        component: () => import('@/views/A')
      },
      {
        path: 'HK',
        meta: { keepAlive: true, needLogin: true },
        component: () => import('@/views/HK')
      },
      {
        path: 'US',
        meta: { keepAlive: true, needLogin: true },
        component: () => import('@/views/US')
      },
      {
        path: 'Plate/:type',
        meta: { needLogin: true },
        component: () => import('@/views/Plate')
      },
      {
        path: 'KechuangPlate',
        meta: { needLogin: true },
        component: () => import('@/views/KechuangPlate')
      },
      {
        path: 'AlarmPrompt',
        meta: { needLogin: true },
        component: () => import('@/views/AlarmPrompt')
      },
      {
        path: 'MemberCharge',
        meta: { needLogin: true },
        component: () => import('@/views/MemberCharge')
      },
      {
        path: 'StrategicWarning',
        meta: { needLogin: true },
        component: () => import('@/views/StrategicWarning')
      },
      {
        path: 'QuantitativeIntoStocks',
        meta: { needLogin: true },
        component: () => import('@/views/QuantitativeIntoStocks'),
        redirect: '/QuantitativeIntoStocks/Edit',
        children: [
          {
            path: 'Edit',
            name: 'Edit',
            meta: { keepAlive: true, needLogin: true },
            component: () => import('@/views/QuantitativeIntoStocks/Edit'),
          },
          {
            path: 'Table',
            name: 'Table',
            meta: { needLogin: true },
            component: () => import('@/views/QuantitativeIntoStocks/Table'),
          },
        ]
      },
      {
        path: '/Customer',
        component: () => import('@/views/Customer')
      },
      {
        path: '/Communication',
        component: () => import('@/views/Communication')
      }
    ]
  },
  {
    path: '/Client',
    component: () => import('@/views/Client')
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  document.title = to.meta.title || '';

  // 获取登录状态
  const TOKEN = store.getters.user.token
  //  
  if (to.meta.needLogin && !TOKEN && to.path != "/Login") {
    next({ path: '/Login' })
  } else {
    next()
  }
})

export default router
